package com.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.sys.entity.SysPersonal;
import com.sys.service.SysPersonalService;
import com.bhudy.base.QueryFilter;
import com.bhudy.enums.Relation;
import com.bhudy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工管理
 *
 * @Author bhudy
 * @Description ResPersonalController
 * @Date Mon Feb 03 10:28:46 CST 2020
 */
@RestController
public class SysPersonalController {

    @Autowired
    private SysPersonalService sysPersonalService;

    /**
     * 获取sysPersonal表的数据
     *
     * @param page  页数{1}
     * @param limit 显示数量{10}
     * @return sysPersonal的list
     */
    @GetMapping(value = "/api/v1/sysPersonals")
    public ResponseResult getAll(Integer page, Integer limit, String name, String sysDepartmentId) {
        QueryFilter queryFilter = new QueryFilter().and("status", Relation.NOT_EQUAL, 1);
        if (page != null && limit != null) queryFilter.limit(--page, limit);
        if (name != null && !name.toString().equals("")) {
            queryFilter.andLike("name", "%" + name + "%");
        }
        if (sysDepartmentId != null && !sysDepartmentId.toString().equals("")) {
            queryFilter.and("sysDepartmentId", sysDepartmentId);
        }
        return ResponseResult.printMapJson(0, sysPersonalService.count(queryFilter), sysPersonalService.findList(queryFilter));
    }

    @GetMapping(value = "/api/v1/sysPersonalJson")
    public Object getAllJson() {
        return ResponseResult.printMapJson(0, sysPersonalService.findMapList("select id, name from sys_personal where status = 0"));
    }

    @GetMapping(value = "/api/v1/sysPersonal")
    public ResponseResult get(Long id) {
        return ResponseResult.printMapJson(0, sysPersonalService.get(id));
    }

    @PostMapping(value = "/api/v1/sysPersonal")
    public ResponseResult insert(@RequestBody String json) {
        SysPersonal sysPersonal = JSONObject.parseObject(json, SysPersonal.class);
        sysPersonalService.insert(sysPersonal);
        return ResponseResult.printMapJson(0, sysPersonal.getId());
    }

    @PutMapping(value = "/api/v1/sysPersonal")
    public ResponseResult update(@RequestBody String json) {
        SysPersonal sysPersonal = JSONObject.parseObject(json, SysPersonal.class);
        sysPersonalService.update(sysPersonal);
        return ResponseResult.printMapJson(0, sysPersonal.getId());
    }
    
    @DeleteMapping(value = "/api/v1/sysPersonals")
    public ResponseResult delete(@RequestBody String json) {
        return ResponseResult.printMapJson(0, sysPersonalService.delete(JSONObject.parseObject(json, String[].class)));
    }

}
